#include "taskwidget.h"

TaskWidget::TaskWidget(QSharedPointer<BackupTask> task_, QWidget *parent) :
    QWidget(parent)
{
    this->task = task_;
    this->setObjectName("taskWidget");

    QGridLayout *centralLayout  = new QGridLayout();
    this->setLayout(centralLayout);

    QLabel *sourceLabel = new QLabel(tr("Source path: "));
    QLabel *sourceValue = new QLabel(task->getSourcePath());
    sourceValue->setObjectName("valueLabel");
    centralLayout->addWidget(sourceLabel, 0, 0);
    centralLayout->addWidget(sourceValue, 0, 1, 1, 3);

    QLabel *targetLabel = new QLabel(tr("Target path: "));
    QLabel *targetValue = new QLabel(task->getTargetPath());
    targetValue->setObjectName("valueLabel");
    centralLayout->addWidget(targetLabel, 1, 0);
    centralLayout->addWidget(targetValue, 1, 1, 1, 3);


    QLabel *cyclicTypeLabel = new QLabel(tr("Backup type: "));
    centralLayout->addWidget(cyclicTypeLabel, 2, 0);
    QLabel *cyclicTypeValue = new QLabel();
    cyclicTypeValue->setObjectName("valueLabel");
    centralLayout->addWidget(cyclicTypeValue, 2, 1);
    if(task->getBackupType() == BackupTask::Complete) {
        cyclicTypeValue->setText(tr("Complete"));
    } else if(task->getBackupType() == BackupTask::Incremental) {
        cyclicTypeValue->setText(tr("Incremental"));
    } else if(task->getBackupType() == BackupTask::Differential) {
        cyclicTypeValue->setText(tr("Differential"));
    }


    if(task->getPeriodic() == true) {
        QLabel *intervalLabel = new QLabel(tr("Interval: "));
        centralLayout->addWidget(intervalLabel, 3, 0);
        QLabel *intervalValue = new QLabel(QString("%1 hours").arg(task->getInterval()));
        intervalValue->setObjectName("valueLabel");
        centralLayout->addWidget(intervalValue, 3, 1);

        QLabel *nextRunLabel = new QLabel(tr("Next run on: "));
        centralLayout->addWidget(nextRunLabel, 3, 2);
        QLabel *nextRunValue = new QLabel(task->getNextRun().toString());
        nextRunValue->setObjectName("valueLabel");
        centralLayout->addWidget(nextRunValue, 3, 3);
    }

    //Remove button
    QPushButton *removeButton = new QPushButton();
    removeButton->setObjectName("removeButton");
    removeButton->setMinimumSize(16, 16);
    removeButton->setMaximumSize(16, 16);
    centralLayout->addWidget(removeButton, 0, 4, Qt::AlignTop);
    connect(removeButton, &QPushButton::clicked, this, &TaskWidget::removeTaskClicked);

    //Instances list
    QLabel *instancesLabel = new QLabel(tr("Backup points: "));
    centralLayout->addWidget(instancesLabel, 4, 0, Qt::AlignHCenter);
    list = new QListWidget();
    list->setObjectName("taskList");
    list->setFocusPolicy(Qt::NoFocus);
    centralLayout->addWidget(list, 4, 1, 1, 3);

    QList<QDateTime> instances = task->getInstances();
    //TODO: MAKE THIS BETTER
    foreach(QDateTime timestamp, instances) {
        QListWidgetItem *item = new QListWidgetItem();
        quint64 instanceSize = task->getInstanceSize(timestamp);
        InstanceWidget *widget = new InstanceWidget(timestamp, instanceSize);
        connect(widget, SIGNAL(deleteInstance(QDateTime)), this, SIGNAL(removeInstanceClicked(QDateTime)));
        item->setSizeHint(widget->sizeHint());
        list->addItem(item);
        list->setItemWidget(item, widget);
        item->setData(Qt::UserRole, timestamp);
    }
    list->setMaximumHeight(list->count()*39);
}

QSharedPointer<BackupTask> TaskWidget::getTask()
{
    return this->task;
}

QListWidget *TaskWidget::getList()
{
    return this->list;
}

bool TaskWidget::taskInstanceSelected()
{
    return !list->selectedItems().isEmpty();
}

QDateTime TaskWidget::getSelectedInstance()
{
    return list->currentItem()->data(Qt::UserRole).toDateTime();
}
